package com.iasku.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;

import android.content.Context;

import com.iasku.constant.Constant;
import com.iasku.iaskueleclass_phone.R;

public class IaskuUtil {
    public static final void parserSchoolInfo(String schoolInfo){
        System.out.println("schoolInfo=>" + schoolInfo);
        try {
            XmlPullParser parser = XmlParserUtil.getXmlPullParser(schoolInfo);
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if ("field".equals(parser.getName())) {
                        if ("serverurl1".equals(parser.getAttributeValue(0))) {
                            Constant.SERVERURL1 = parser.nextText();
                        } else if ("serverurl2".equals(parser.getAttributeValue(0))) {
                            Constant.SERVERURL2 = parser.nextText();
                        } else if ("airserver1".equals(parser.getAttributeValue(0))) {
                            Constant.AIRSERVER1 = parser.nextText();
                        } else if ("airserver2".equals(parser.getAttributeValue(0))) {
                            Constant.AIRSERVER2 = parser.nextText();
                        } else if ("decodemode".equals(parser.getAttributeValue(0))){
                            Constant.DECODE_MODE = parser.nextText();
                        } else if ("dzcs".equals(parser.getAttributeValue(0))){
                            Constant.ERROR_TIMES = Integer.parseInt(parser.nextText()) + 1;
                        }
                    }
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Constant.SERVERURL = Constant.SERVERURL2;
        new Thread() {
            public void run() {
                String url = Constant.SERVERURL1 + "/api/GetMyHomework";
                StringBuffer buf = new StringBuffer();
                buf.append("?username=").append(Constant.USER_IASKUNAME);
                buf.append("&subjectname=").append(0);
                buf.append("&state=").append(0);
                buf.append("&time1=").append(System.currentTimeMillis() / 1000 - 7 * 24 * 60 * 60);
                buf.append("&time2=").append(System.currentTimeMillis() / 1000);
                HttpClient httpClient = new DefaultHttpClient();
                String uri = url + buf.toString();
                HttpGet httpGet = new HttpGet(uri);
                try {
                    HttpResponse response = httpClient.execute(httpGet);
                    if (response.getStatusLine().getStatusCode() == 200) {
                        HttpEntity httpEntity = response.getEntity();
                        String result = EntityUtils.toString(httpEntity);
                        if (result != null) {
                            Constant.SERVERURL = Constant.SERVERURL1;
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 

            }
        }.start();
    }

    public static String getSubjectName(Context mContext, int subjectId) {
        String[] subjects = mContext.getResources().getStringArray(R.array.subject_all_list);
        return subjects[subjectId];
    }
    
    public static String getQuestionTypeName(Context mContext, int questionTypeId) {
        String[] questionTypes = mContext.getResources().getStringArray(R.array.questiontype_name_list);
        return questionTypes[questionTypeId];
    }
    
    public static int getSubjectId(Context mContext,String subjectname){
        String[] subjects = mContext.getResources().getStringArray(R.array.subject_all_list);
        for(int i = 0; i < subjects.length; i++){
            if(subjectname.equals(subjects[i])){
                return i;
            }
        }
        return 0;
    }
    
    public static int getQuestionTypeId(Context mContext,String questionTypeName){
        String[] questionTypes = mContext.getResources().getStringArray(R.array.questiontype_name_list);
        for(int i = 0; i < questionTypes.length; i++) {
            if(questionTypeName.equals(questionTypes[i])) {
                return i;
            }
        }
        return 0;
    }
}
